package cn.ipanel.bigdata.dw

import cn.ipanel.bigdata.boot.config.Configuration
import cn.ipanel.bigdata.boot.date.Day
import cn.ipanel.bigdata.boot.source.genre.Hive
import cn.ipanel.bigdata.comm.Parameter.DIM_DB_NAMESPACE
import cn.ipanel.bigdata.utils.Dictionary
import org.apache.spark.sql.DataFrame
import org.apache.spark.sql.functions.{col, lit}

/**
 * Author: lzz
 * Date: 2022/3/24 19:42
 */
package object dim {

  final lazy val DIM_DB_NAME: String = Configuration.getParam(DIM_DB_NAMESPACE)
  final lazy val BUILD: String = "create database if not exists " + DIM_DB_NAME

  abstract class AbsDimHive(tbName: String)
          extends Hive(DIM_DB_NAME, tbName)
            with Const {

    final val F_DATE: String = Dictionary.F_DATE

    def find(day: Day): DataFrame = {
      load.filter(col(F_DATE) === lit(day.toDate))
    }

    def drop(day: Day): Unit = {
      exec(s"alter table $getDBName.$getTBName drop if exists partition($F_DATE=${day.toDate})")
    }
  }
}